package com.aube.plugin.gallery;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.CategorySimple;
import com.huyn.baseframework.model.Picture;
import com.huyn.baseframework.utils.ImageParamUtils;
import com.huyn.baseframework.utils.SysUtil;

import java.util.List;

/**
 * Created by huyaonan on 16/6/17.
 */
public class GalleryView extends DLLayout {

    private int mSpace=0;
    private int mItemHeight=0;
    private int mTotalWidth=0;
    private int mItemSpace=10;
    private int mScreenWidth=0;
    private int mAnimCount=0;
    private int[] mAnimDelta;

    private OnGalleryItemClickListener mListener;

    public GalleryView(Context context) {
        this(context, null);
    }
    public GalleryView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public GalleryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        mSpace = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        mScreenWidth = SysUtil.getAbsoluteScreenWidth(context);
        mItemHeight = mScreenWidth/3;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = mTotalWidth + getPaddingLeft() + getPaddingRight();
        int height = mItemHeight + getPaddingTop() + getPaddingBottom();
        super.onMeasure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
//        for (int i = 0; i < getChildCount(); i++) {
//            getChildAt(i).measure(MeasureSpec.makeMeasureSpec(mItemWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mItemHeight, MeasureSpec.EXACTLY));
//        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int xPos = 0;
        int itemWidth = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            itemWidth = child.getMeasuredWidth();
            getChildAt(i).layout(xPos, top, xPos+itemWidth, top + mItemHeight);
            xPos+=mItemSpace+itemWidth;
        }
    }

    private int getItemWidth(Picture pic) {
        if(pic.getWidth() > pic.getHeight())
            return mItemHeight*4/3;
        else if(pic.getWidth() < pic.getHeight())
            return mItemHeight*3/4;
        else
            return mItemHeight;
    }

    public void anim(int delay) {
        if(mAnimDelta != null && mAnimDelta.length > mAnimCount) {
            int index=0;
            for(final int i : mAnimDelta) {
                final View view = getChildAt(index);
                view.setAlpha(0f);
                final ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();

                        view.setAlpha(value);
                        view.setTranslationX(i*(1-value));
                    }
                });
                anim.setDuration(300);
                anim.setStartDelay(delay+200*index);
                anim.start();
                index++;
            }
        }
    }

    @Override
    public void fillData(Object o) {
    }

    public void fillData(final CategorySimple cate, final List<PictureImp> list, int count) {
        int size = list.size();
        int mItemWidth = 0;

        mAnimDelta = new int[size];
        boolean state=true;
        for(int i=0; i<size; i++) {
            final Picture pic = list.get(i);
            final ImageView img = new ImageView(context);
            img.setScaleType(ImageView.ScaleType.CENTER_CROP);

            mItemWidth = getItemWidth(pic);
            mTotalWidth+=mItemWidth;

            final int index = i;
            addView(img, new ViewGroup.LayoutParams(mItemWidth, mItemHeight));
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (mHostClickListener != null) {
//                        mHostClickListener.onCLickToPreview(img, list, index, false);
//                    }
                    if(mListener != null)
                        mListener.onClickToPreview(img, list, index);
                }
            });
//            img.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bk_default));
//            ImageLoader.getInstance(context).fillImage(img, pic.getPictureUrl(), 400, 400);
            ImageLoader.getInstance(context).preloadBitmap(ImageParamUtils.getScaledImgUrl(pic.getPictureUrl(), pic.getWidth(), pic.getHeight(), 400), new ImageLoader.IPreloadCallback<Bitmap>() {
                @Override
                public void onSuccess(Bitmap bitmap) {
                    img.setImageBitmap(bitmap);
                }

                @Override
                public void onFail() {

                }
            });

            if(i != size-1) {
                mTotalWidth+=mItemSpace;
            }

            if(state) {
                mAnimDelta[i] = mItemWidth/2;
                if(mTotalWidth > mScreenWidth) {
                    state = false;
                    mAnimCount = i;
                }
            }
        }

        if(count > list.size()) {
            View view = inflate(R.layout.simple_text, null, false);
            TextView mMore = (TextView) view.findViewById(R.id.more_pic);
            mTotalWidth += mItemSpace + mItemHeight;
            addView(view, new ViewGroup.LayoutParams(mItemHeight, mItemHeight));
            mMore.setText("查看全部" + count + "张图片");
            mMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (mHostClickListener != null)
//                        mHostClickListener.onClickToGallery(cate);
                    if(mListener != null)
                        mListener.onClickToList(cate);
                }
            });
        }
    }

    @Override
    public void notifyAnim(int animDelay) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }

    public void setOnGalleryItemClickListener(OnGalleryItemClickListener listener) {
        this.mListener = listener;
    }

}
